﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KCMS.DataAcsess.Interfaces;
using StructureMap;
using AutoMapper;
using KCMS.BusinessLogic.Models;
using KCMS.BusinessLogic.Interfaces;


namespace KCMS.BusinessLogic.Engine
{
    public class NewsService:INewsService
    {
        private INewsManager newsManager = ObjectFactory.GetInstance<INewsManager>();
        public Models.News getById(Guid id)
        {
            DataAcsess.Entitys.NewsEntity newsEntity = newsManager.getById(id);
            return Mapper.Map<DataAcsess.Entitys.NewsEntity, Models.News>(newsEntity);
        }
       
        public IEnumerable<Models.News> getAll()
        {
            var model = from t in newsManager.getAll()
                        select t;
            return Mapper.Map<IEnumerable<DataAcsess.Entitys.NewsEntity>, IEnumerable<Models.News>>(model);

        }

        public void update(News news)
        {
            newsManager.update(Mapper.Map<News, DataAcsess.Entitys.NewsEntity>(news));
        }


        public void save(News news)
        {
            news.Date = DateTime.Now;
            newsManager.save(Mapper.Map<News, DataAcsess.Entitys.NewsEntity>(news));
        }


        public void delete(Guid id)
        {
            newsManager.delete(id);
        }


        public News getByCode(string code)
        {
            return (from t in getAll()
                    where t.Code.Equals(code)
                    select t).SingleOrDefault();
        }


        public IEnumerable<News> getAllPublised()
        {
            return (from t in getAll()
                    where t.Published.Equals(true)
                    select t).ToList();
        }
    }
}
